package com.sca;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.nacos.client.naming.utils.Chooser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;
/*
* 这个注解启动时去找到该注解秒数据接口。斌基于接口有代理。创建接口对象
*
* */

@EnableFeignClients
@SpringBootApplication
public class ConsumerRun {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerRun.class, args);
    }
    @Value("${server.port}")
    private String server;
    /**构建远程过程调用对象,基于此对象访问远程服务*/
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
   @LoadBalanced
    @Bean
    public RestTemplate loadBalanceRestTemplate(){
        return new RestTemplate();
    }

    @Autowired
    private RestTemplate loadBalanceRestTemplate;

    @RestController
    public class ConsumerController{
        //基于此对象从注册中心获取服务实例对象
        @Autowired
        private LoadBalancerClient loadBalancerClient;

        @Autowired
        private RestTemplate restTemplate;
        //AtomicLong 这个类支持线程安全的自增自建操作，线程安全的
        private AtomicLong aLong=new AtomicLong(1);

        @GetMapping("/echo1")
        public String doRestEcho01() throws InterruptedException {
            long num = aLong.getAndDecrement();
           /* if (num%2 == 0) {  //模拟50%的慢调用比例
               //慢调用比例熔断
                // Thread.sleep(200);
                //System.out.println(num);
                //异常比例熔断
                throw new RuntimeException("出现异常了");
            }*/
            String url="http://localhost:8081/provider/echo/"+server;
            //远程过程调用-RPC
            return restTemplate.getForObject(url,String.class);
        }

        //设置我们的热点链路 的数据的限流
        @GetMapping("/echo5/id")
        @SentinelResource("findId")
        public String doFindById(@RequestParam("id") String id){

            return "resouce id is " +id;
        }





        @GetMapping("/consumer/doRestEcho02")
        public String doRestEcho02(){
            //基于服务名（nacos中的服务列表的名字）查钊实例服务
            ServiceInstance choose = loadBalancerClient.choose("sca-provider");
            String ip= choose.getHost();
            int port=choose.getPort();
            //远程过程的调用-RPC
            String url="http://"+ip+":"+port+"/provider/echo/" +server;

           // String url=""+server;
            //远程过程调用-RPC
            return restTemplate.getForObject(url,String.class);
        }

        @GetMapping("/consumer/doRestEcho03")
        public String doRestEcho03(){

            String url=String.format("http://%s/provider/echo/%s","sca-provider",server);
            // String url=""+server;
            //远程过程调用-RPC
            return loadBalanceRestTemplate.getForObject(url,String.class);
        }

    }
}
